package com.winit.core.host.power;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.util.Constants;
import com.winit.common.util.ObjectToString;
import com.winit.core.host.power.beans.PowerParam;
import com.winit.core.host.power.beans.PowerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Created by wangjian on 16/7/22.
 * 查询角色权限
 *
 * rolePower.json文件说明
 * 角色权限配置,若不配置,默认无权限  0有权限  1无权限
 ＊ 1.menu
 * 首页菜单显示权限
 * 2.env
 * 环境操作权限,用于环境列表展示,可选不可选
 */
@Component
public class RolePower {

    private static final Logger logger = LoggerFactory.getLogger(RolePower.class);

    private String OWN_POWER = "0";//有权限
    private String NOT_OWN_POWER = "1";//无权限


    public boolean getPower(PowerParam powerParam){
        logger.info("查询角色权限,powerParam="+ ObjectToString.getString(powerParam));
        boolean isPower = false;
        try {
            Assert.hasText(powerParam.getName(),"查询角色权限,参数不能为空");
            Assert.hasText(powerParam.getUserRole(),"查询角色权限,参数不能为空");
            Assert.hasText(powerParam.getPowerType().getDesc(),"查询角色权限,参数不能为空");
            if(Constants.USER_ROLE_ADMIN.equals(powerParam.getUserRole())){
                logger.info("查询角色权限,powerParam="+ ObjectToString.getString(powerParam)+",return=true");
                return true;
            }

            List<JSONObject> list = Constants.ROLE_POWER_MAP.get(powerParam.getPowerType().getDesc());
            switch (powerParam.getPowerType()){
                case MENU:
                    break;
                case ENV:
                    for(JSONObject jsonObject:list){
                        if(jsonObject.get("role").equals(powerParam.getUserRole())){
                            JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("power"));
                            String env = powerParam.getName().split("\\.")[0].toUpperCase();
                            for (int i=0;i<jsonArray.size();i++){
                                JSONObject object = (JSONObject) jsonArray.get(i);
                                if(object.get("key").equals(env)){
                                    isPower = OWN_POWER.equals(object.get("value"));
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    break;
                default:
                    logger.error("查询角色权限,参数错误");
                    break;
            }
        }catch (Exception e){
            logger.error("查询角色权限异常,powerParam="+ ObjectToString.getString(powerParam),e);
        }
        logger.info("查询角色权限,powerParam="+ ObjectToString.getString(powerParam)+",return="+isPower);
        return isPower;
    }

    public static void main(String[] args) {
        RolePower rolePower = new RolePower();
        PowerParam powerParam = new PowerParam();
        powerParam.setName("qa.1");
        powerParam.setPowerType(PowerType.ENV);
        powerParam.setUserRole(Constants.USER_ROLE_QA);

        rolePower.getPower(powerParam);
    }
}
